<template>
  <div class="app-container">
    <!--    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="员工姓名" prop="empName">
        <el-input
          v-model="queryParams.empName"
          placeholder="请输入员工姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出勤日期" prop="onWorkAm">
        <el-date-picker clearable
                        v-model="queryParams.onWorkAm"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="--" prop="offWorkAm">
        <el-date-picker clearable
                        v-model="queryParams.offWorkAm"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="全勤选项" prop="isQualified">
        <el-checkbox v-model="queryParams.isQualified"></el-checkbox>
      </el-form-item>



      <!--            级联选择器_头-->
<!--      <el-form-item label="选择状态" prop="offWorkAm">-->
<!--        <div class="block">-->
<!--          <el-cascader-->
<!--            clearable-->
<!--            v-model="queryParams.isQualified"-->
<!--            :options="options"-->
<!--            @change="handleChange"></el-cascader>-->
<!--        </div>-->
<!--      </el-form-item>-->
      <!--            级联选择器_尾-->
<!--      <el-form-item label="选择状态" prop="offWorkAm">-->
<!--        <div class="block">-->
<!--          <el-cascader-->
<!--            clearable-->
<!--            v-model="value"-->
<!--            :options="options"-->
<!--            @change="handleChange"></el-cascader>-->
<!--        </div>-->
<!--      </el-form-item>-->



      <!--      <el-form-item label="下午上班时间" prop="onWorkPm">-->
      <!--        <el-date-picker clearable-->
      <!--          v-model="queryParams.onWorkPm"-->
      <!--          type="date"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          placeholder="请选择下午上班时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="上午下班时间" prop="offWorkPm">-->
      <!--        <el-date-picker clearable-->
      <!--          v-model="queryParams.offWorkPm"-->
      <!--          type="date"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          placeholder="请选择上午下班时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="记录时间" prop="recordTime">-->
      <!--        <el-date-picker clearable-->
      <!--                        v-model="queryParams.recordTime"-->
      <!--                        type="date"-->
      <!--                        value-format="yyyy-MM-dd"-->
      <!--                        placeholder="请选择记录时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--    添加按钮-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          round
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          v-hasPermi="['attendance:attendance:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          round
          icon="el-icon-edit"
          size="small"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['attendance:attendance:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          round
          icon="el-icon-delete"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['attendance:attendance:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          round
          icon="el-icon-download"
          size="small"
          @click="handleExport"
          v-hasPermi="['check:attendance:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="attendanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="出勤编号" align="center" prop="atId"/>
      <el-table-column label="员工编号" align="center" prop="empId"/>
      <el-table-column label="员工姓名" align="center" prop="empName"/>
      <el-table-column label="上午上班时间(08:00)" align="center" prop="onWorkAm" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.onWorkAm, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          <el-tag type="success" v-if="scope.row.onWorkAmMsg == '合格'">{{ scope.row.onWorkAmMsg }}</el-tag>
          <el-tag type="danger" v-if="scope.row.onWorkAmMsg == '迟到'">{{ scope.row.onWorkAmMsg }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="上午下班时间(12:00)" align="center" prop="offWorkAm" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.offWorkAm, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          <el-tag type="success" v-if="scope.row.offWorkAmMsg == '合格'">{{ scope.row.offWorkAmMsg }}</el-tag>
          <el-tag type="warning" v-if="scope.row.offWorkAmMsg == '早退'">{{ scope.row.offWorkAmMsg }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="下午上班时间(14:00)" align="center" prop="onWorkPm" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.onWorkPm, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          <el-tag type="success" v-if="scope.row.onWorkPmMsg == '合格'">{{ scope.row.onWorkPmMsg }}</el-tag>
          <el-tag type="danger" v-if="scope.row.onWorkPmMsg == '迟到'">{{ scope.row.onWorkPmMsg }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="上午下班时间(17:30)" align="center" prop="offWorkPm" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.offWorkPm, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          <el-tag type="success" v-if="scope.row.offWorkPmMsg == '合格'">{{ scope.row.offWorkPmMsg }}</el-tag>
          <el-tag type="warning" v-if="scope.row.offWorkPmMsg == '早退'">{{ scope.row.offWorkPmMsg }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="记录时间" align="center" prop="recordTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.recordTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['attendance:attendance:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['attendance:attendance:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改出勤管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--        员工编号改下拉框-->
        <el-form-item label="员工姓名" prop="empId">
          <!--          <el-input v-model="form.empId" placeholder="请输入员工编号" />-->
          <el-select v-model="form.empId" placeholder="请选择">
            <el-option
              v-for="item in employee"
              :key="item.empId"
              :label="item.empName"
              :value="item.empId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="上午上班时间" prop="onWorkAm">
          <el-date-picker clearable
                          v-model="form.onWorkAm"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择上午上班时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="上午下班时间" prop="offWorkAm">
          <el-date-picker clearable
                          v-model="form.offWorkAm"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择上午下班时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="下午上班时间" prop="onWorkPm">
          <el-date-picker clearable
                          v-model="form.onWorkPm"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择下午上班时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="上午下班时间" prop="offWorkPm">
          <el-date-picker clearable
                          v-model="form.offWorkPm"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择上午下班时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="记录时间" prop="recordTime">
          <el-date-picker clearable
                          v-model="form.recordTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择记录时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAttendance,
  getAttendance,
  delAttendance,
  addAttendance,
  updateAttendance,
  listEmployee
} from "@/api/check/attendance";

export default {
  name: "Attendance",
  data() {
    return {
      //value: [],
      options: [{
        value: '上午上班时间',
        label: '上午上班时间',
        children: [{
          value: '合格',
          label: '合格',
        }, {
          value: '迟到',
          label: '迟到',
        }]
      },{
        value: '上午下班时间',
        label: '上午下班时间',
        children: [{
          value: '合格',
          label: '合格',
        }, {
          value: '早退',
          label: '早退',
        }]
      },{
        value: '下午上班时间',
        label: '下午上班时间',
        children: [{
          value: '合格',
          label: '合格',
        }, {
          value: '迟到',
          label: '迟到',
        }]
      },{
        value: '下午下班时间',
        label: '下午下班时间',
        children: [{
          value: '合格',
          label: '合格',
        }, {
          value: '早退',
          label: '早退',
        }]
      }],


      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出勤管理表格数据
      attendanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        empId: null,
        onWorkAm: null,
        offWorkAm: null,
        onWorkPm: null,
        offWorkPm: null,
        recordTime: null,
        // 添加数据
        emp_id: null,
        empName: null,
        // 判断时间是否合格
        isQualified:false,
      },
      employee: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        empId: [
          {required: true, message: "员工姓名不能为空", trigger: "blur"}
        ],
        onWorkAm: [
          {required: true, message: "上午上班时间不能为空", trigger: "blur"}
        ],
        offWorkAm: [
          {required: true, message: "上午下班时间不能为空", trigger: "blur"}
        ],
        onWorkPm: [
          {required: true, message: "下午上班时间不能为空", trigger: "blur"}
        ],
        offWorkPm: [
          {required: true, message: "下午下班时间不能为空", trigger: "blur"}
        ],
        recordTime: [
          {required: true, message: "记录时间不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    /** 查询出勤管理列表 */
    getList() {
      this.loading = true;
      listAttendance(this.queryParams).then(response => {
        this.attendanceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        atId: null,
        empId: null,
        onWorkAm: null,
        offWorkAm: null,
        onWorkPm: null,
        offWorkPm: null,
        recordTime: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // if(this.isQualified==null){
      //   this.isQualified = ["12","12"];
      //   console.log(this.isQualified)
      // }
      this.queryParams.pageNum = 1;
      console.log("练级选择器数据："+this.value)
      console.log("练级选择器数据类型："+ typeof  this.value)
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.atId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加出勤管理";

      // 发送请求
      listEmployee().then(response => {
        this.employee = response.rows;
        this.loading = false;
      });

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const atId = row.atId || this.ids
      getAttendance(atId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改出勤管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.atId != null) {
            //修改逻辑
            let axiosPromise_update = updateAttendance(this.form);
            axiosPromise_update.then(res => {
              if (res) {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.$modal.msgError("时间字段应为同一天");
              }
            });
            // updateAttendance(this.form).then(response => {
            //   this.$modal.msgSuccess("修改成功");
            //   this.open = false;
            //   this.getList();
            // });
          } else {
            //新增逻辑
            let axiosPromise_add = addAttendance(this.form);
            axiosPromise_add.then((res) => {
              if (res) {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.$modal.msgError("时间字段应为同一天");
              }
            })

            // addAttendance(this.form).then(response => {
            //   this.$modal.msgSuccess("新增成功");
            //   this.open = false;
            //   this.getList();
            // });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const atIds = row.atId || this.ids;
      this.$modal.confirm('是否确认删除出勤管理编号为"' + atIds + '"的数据项？').then(function () {
        return delAttendance(atIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('check/attendance/export', {
        ...this.queryParams
      }, `attendance_${new Date().getTime()}.xlsx`)
    }
  },
  mounted() {
    // 发送查询所有员工的请求
    listEmployee().then(response => {
      this.employee = response.rows;
      console.log("调用生命周期函数：" + this.employee)

      this.loading = false;
    });
  }
};
</script>
